package org.jeecg.modules.earlywarning.rules.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.earlywarning.rules.entity.EwRules;
import org.jeecg.modules.earlywarning.rules.service.IEwRulesService;
import org.jeecg.modules.equipment.gasmeter.entity.DmGasmeter;
import org.jeecg.modules.equipment.gasmeter.service.IDmGasmeterService;
import org.jeecg.modules.equipment.gasmeter.util.BulletUtil;
import org.jeecg.modules.equipment.steammeter.entity.DmSteammeter;
import org.jeecg.modules.equipment.steammeter.service.IDmSteammeterService;
import org.jeecg.modules.equipment.watermeter.entity.DmWatermeter;
import org.jeecg.modules.equipment.watermeter.service.IDmWatermeterService;
import org.jeecg.modules.equipment.watthourmeter.entity.DmWatthourmeter;
import org.jeecg.modules.equipment.watthourmeter.service.IDmWatthourmeterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 预警规则
 * @Author: 贾送兵
 * @Date: 2021-11-30
 * @Version: V1.0
 */
@Api(tags = "预警规则")
@RestController
@RequestMapping("/rules/ewRules")
@Slf4j
public class EwRulesController extends JeecgController<EwRules, IEwRulesService> {
    @Autowired
    private IEwRulesService ewRulesService;
    @Autowired
    private IDmGasmeterService dmGasmeterService;//燃气表管理
    @Autowired
    private IDmWatthourmeterService dmWatthourmeterService;//电表管理啊

    @Autowired
    private IDmWatermeterService dmWatermeterService;//水表管理

    @Autowired
    private IDmSteammeterService dmSteammeterService;//蒸汽表管理

    /**
     * 分页列表查询
     *
     * @param ewRules
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "预警规则-分页列表查询")
    @ApiOperation(value = "预警规则-分页列表查询", notes = "预警规则-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(EwRules ewRules,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String tenantId = req.getHeader("tenant-id");
        //获取到的租户id为0的情况下是admin管理员是可以看到全部的
        if (!tenantId.equals("0")) {
            ewRules.setTenantId(tenantId);
        }
        QueryWrapper<EwRules> queryWrapper = QueryGenerator.initQueryWrapper(ewRules, req.getParameterMap());


        Page<EwRules> page = new Page<EwRules>(pageNo, pageSize);
        IPage<EwRules> pageList = ewRulesService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * @param req
     * @param code 根据能源标识查询对应能源表，例如查询燃气能源设备输入“RQ”
     * @return
     */
    @AutoLog(value = "查询设备")
    @ApiOperation(value = "查询设备", notes = "查询设备")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "code", value = "能源类型标识，燃气输入RQ，蒸汽输入ZQ，水输入S，电输入DB", dataType = "String", required = false),
    })
    @GetMapping(value = "/queryList")
    public Result<?> queryList(HttpServletRequest req, @RequestParam(name = "code") String code) {


		String tenantId = req.getHeader("tenant-id");
		DmGasmeter dmGasmeter = new DmGasmeter();
		DmSteammeter dmSteammeter = new DmSteammeter();
		DmWatermeter dmWatermeter = new DmWatermeter();
		DmWatthourmeter dmWatthourmeter = new DmWatthourmeter();

		//获取到的租户id为0的情况下是admin管理员是可以看到全部的
		if (!tenantId.equals("0")) {
			dmGasmeter.setTenantId(tenantId);
			dmSteammeter.setTenantId(tenantId);
			dmWatermeter.setTenantId(tenantId);
			dmWatthourmeter.setTenantId(tenantId);
		}


		QueryWrapper<DmGasmeter> dmGasmeterWrapper = QueryGenerator.initQueryWrapper(dmGasmeter, req.getParameterMap());

		QueryWrapper<DmSteammeter> dmSteammeterWrapper = QueryGenerator.initQueryWrapper(dmSteammeter, req.getParameterMap());

		QueryWrapper<DmWatermeter> dmWatermeterWrapper = QueryGenerator.initQueryWrapper(dmWatermeter, req.getParameterMap());

		QueryWrapper<DmWatthourmeter> dmWatthourmeterWrapper = QueryGenerator.initQueryWrapper(dmWatthourmeter, req.getParameterMap());


		switch (code) {
            case "RQ":
                List<DmGasmeter> dmglist = dmGasmeterService.list(dmGasmeterWrapper);
                return Result.OK(dmglist);
            case "ZQ":
				List<DmSteammeter>  d = dmSteammeterService.list(dmSteammeterWrapper);
                return Result.OK(d);
            case "S":
                List<DmWatermeter> m = dmWatermeterService.list(dmWatermeterWrapper);
                return Result.OK(m);
            case "DB":
                List<DmWatthourmeter> t = dmWatthourmeterService.list(dmWatthourmeterWrapper);
                return Result.OK(t);
            default:
                return Result.error("没有查询到数据");
        }
    }


    @AutoLog(value = "查询所有设备")
    @ApiOperation(value = "查询所有设备", notes = "查询所有设备")
    @GetMapping(value = "/queryeQuipmentList")
    public Result<?> queryeQuipmentList(HttpServletRequest req) {

        DmGasmeter dmGasmeter = new DmGasmeter();
        DmSteammeter dmSteammeter = new DmSteammeter();
        DmWatermeter dmWatermeter = new DmWatermeter();
        DmWatthourmeter dmWatthourmeter = new DmWatthourmeter();

        //存入租户id
        dmGasmeter.setTenantId(BulletUtil.getTenantId(req));
        //存入租户id
        dmSteammeter.setTenantId(BulletUtil.getTenantId(req));
        //存入租户id
        dmWatermeter.setTenantId(BulletUtil.getTenantId(req));
        //存入租户id
        dmWatthourmeter.setTenantId(BulletUtil.getTenantId(req));


        QueryWrapper<DmGasmeter> dmGasmeterWrapper = QueryGenerator.initQueryWrapper(dmGasmeter, req.getParameterMap());

        QueryWrapper<DmSteammeter> dmSteammeterWrapper = QueryGenerator.initQueryWrapper(dmSteammeter, req.getParameterMap());

        QueryWrapper<DmWatermeter> dmWatermeterWrapper = QueryGenerator.initQueryWrapper(dmWatermeter, req.getParameterMap());

        QueryWrapper<DmWatthourmeter> dmWatthourmeterWrapper = QueryGenerator.initQueryWrapper(dmWatthourmeter, req.getParameterMap());

        //查询燃气
        List<DmGasmeter> dmglist = dmGasmeterService.list(dmGasmeterWrapper);
        //查项蒸汽
        List<DmSteammeter> dmSteammeterlist = dmSteammeterService.list(dmSteammeterWrapper);
        //查询水
        List<DmWatermeter> dmWatermeterlist = dmWatermeterService.list(dmWatermeterWrapper);
        //查询电表
        List<DmWatthourmeter> dmWatthourmeterlist = dmWatthourmeterService.list(dmWatthourmeterWrapper);
        //组装数据
        Map<String, List> map = new HashMap<>();
        map.put("RQ", dmglist);
        map.put("ZQ", dmSteammeterlist);
        map.put("S", dmWatermeterlist);
        map.put("DB", dmWatthourmeterlist);
        return Result.OK(map);
    }


    /**
     * 添加
     *
     * @param ewRules
     * @return
     */
    @AutoLog(value = "预警规则-添加")
    @ApiOperation(value = "预警规则-添加", notes = "预警规则-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody EwRules ewRules, HttpServletRequest req) {
        //存入租户id
        ewRules.setTenantId(BulletUtil.getTenantId(req));
        ewRulesService.save(ewRules);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param ewRules
     * @return
     */
    @AutoLog(value = "预警规则-编辑")
    @ApiOperation(value = "预警规则-编辑", notes = "预警规则-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody EwRules ewRules) {
        ewRulesService.updateById(ewRules);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "预警规则-通过id删除")
    @ApiOperation(value = "预警规则-通过id删除", notes = "预警规则-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        ewRulesService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "预警规则-批量删除")
    @ApiOperation(value = "预警规则-批量删除", notes = "预警规则-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ewRulesService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "预警规则-通过id查询")
    @ApiOperation(value = "预警规则-通过id查询", notes = "预警规则-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        EwRules ewRules = ewRulesService.getById(id);
        if (ewRules == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(ewRules);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ewRules
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EwRules ewRules) {
        return super.exportXls(request, ewRules, EwRules.class, "预警规则");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EwRules.class);
    }

}
